package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.ILogoutService;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.mall.user.intercepter.TokenIntercepter.ACCESS_TOKEN;

/**
 * @Author: DLX
 * @Date: 2021/6/21 2:00
 * @Description:
 **/
@RestController
@RequestMapping("/user")
public class LogoutController {


//    @Reference
//    ILogoutService logoutService;

    @GetMapping("/loginOut")
    public ResponseData logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
//        String token = CookieUtil.getCookieValue(httpServletRequest, ACCESS_TOKEN);
        String userInfo = ((String) httpServletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY));
        if (userInfo != null) {
//            logoutService.logout();
            Cookie cookie= CookieUtil.genCookie("access_token",null,"/",6000000);
            cookie.setHttpOnly(true);
            httpServletResponse.addCookie(cookie);
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg("token已失效2");
    }
}
